package com.situ.servlet.movie;

import com.alibaba.fastjson2.JSON;
import com.situ.pojo.Movie;
import com.situ.pojo.search.MovieDTO;
import com.situ.service.MovieService;
import com.situ.service.impl.MovieServiceImpl;
import com.situ.util.BeanFactory;
import com.situ.util.Pagination;
import com.situ.util.ServletUtils;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/movie/*")
public class MovieServlet extends HttpServlet {
    private MovieService movieService= BeanFactory.getBean(MovieServiceImpl.class);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getPathInfo();
        if ("/list".equals(action )){
            RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/jsp/movie/list.jsp");
            dispatcher.forward(req,resp);
        } else if ("/add".equals(action)) {
            RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/jsp/movie/add.jsp");
            dispatcher.forward(req,resp);
        }
        else if ("/edit".equals(action)) {
            String id = req.getParameter("id");
            req.setAttribute("id",id);
//            System.out.println(id);
            RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/jsp/movie/edit.jsp");
            dispatcher.forward(req,resp);
        }
        else  if ("/id".equals( action)){
            String id = req.getParameter("id");
            Movie movie = movieService.findById(Integer.valueOf(id));
            System.out.println(movie);
            ServletUtils.renderJson(resp,Map.of("movie",movie));
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getPathInfo();
        if ("/list".equals(action)){
//            System.out.println("dianyingliebiao");
            list(req,resp);
        }
        else if ("/add".equals(action)) {
            add(req,resp);
        } else if ("/edit".equals(action)) {
            edit(req,resp);
        } else if ("/delete".equals(action)) {
            deleteByIds(req,resp);

        }
    }
    private void list(HttpServletRequest req, HttpServletResponse resp){
        String page = req.getParameter("page");//当前页码
        String limit = req.getParameter("limit");//每页展示的条数
        //查询条件
        String name = req.getParameter("name");
        String actor = req.getParameter("actor");
        String startTime= req.getParameter("startTime");
        String endTime= req.getParameter("endTime");
        String rating=req.getParameter("rating");
        String movieTime=req.getParameter("movieTime");
        String order = req.getParameter("order");
        Double dRating=null;
//        System.out.println(endTime+startTime);
        if (rating!=null&&rating.length()>0){
             dRating = Double.valueOf(rating);

        }else {
            dRating=null;
        }
        MovieDTO movieDTO = new MovieDTO();
        if (StringUtils.hasText( name)){
            movieDTO.setName(name);
        }
        if (StringUtils.hasText( actor)){
            movieDTO.setActor(actor);
        }
        if (StringUtils.hasText( startTime)){
            movieDTO.setStartTime(LocalDate.parse(startTime));
        }
        if (StringUtils.hasText( endTime)){
            movieDTO.setEndTime(LocalDate.parse(endTime));
        }
        if (StringUtils.hasText( movieTime)){
            movieDTO.setMovieTime(Integer.valueOf(movieTime));
        }
        if (StringUtils.hasText( order)){
            movieDTO.setOrder(order);
        }

        movieDTO.setRating(dRating);
//        System.out.println(dRating);
        Pagination pagination=null;
        try {
            pagination = new Pagination(Integer.parseInt(page),Integer.parseInt(limit));
        } catch (NumberFormatException e) {
            pagination = new Pagination(1,20);
        }
        List<Movie> movieList=movieService.list(movieDTO,pagination);
//        System.out.println(movieList);

        Map<String,Object> map = Map.of("movieList", movieList,"pi",pagination);

        resp.setContentType("application/json;charset=UTF-8");
        String json=JSON.toJSONString(map);
        PrintWriter out=null;

        try {
            out = resp.getWriter();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //将响应内容修改为JSON
        out.write(json);
        out.flush();
    }
    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String actor = req.getParameter("actor");
        String dateTime = req.getParameter("dateTime");
        String movieTime = req.getParameter("movieTime");
        String rating = req.getParameter("rating");
        String money = req.getParameter("money");
        Movie movie = new Movie();
        movie.setName(name);
        movie.setActor(actor);
        movie.setDateTime(LocalDate.parse(dateTime));
        movie.setMovieTime(Integer.valueOf(movieTime));
        movie.setRating(Double.valueOf(rating));
        movie.setMoney(Double.valueOf(money));
        Boolean add=movieService.add(movie);
        ServletUtils.renderJson(resp,Map.of("success",add));
    }
    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String actor = req.getParameter("actor");
        String dateTime = req.getParameter("dateTime");
        String movieTime = req.getParameter("movieTime");
        String rating = req.getParameter("rating");
        String money = req.getParameter("money");
        Movie movie = new Movie();
        movie.setId(Integer.valueOf(id));
        movie.setName(name);
        movie.setActor(actor);
        movie.setDateTime(LocalDate.parse(dateTime));
        movie.setMovieTime(Integer.valueOf(movieTime));
        movie.setRating(Double.valueOf(rating));
        movie.setMoney(Double.valueOf(money));
        Boolean edit=movieService.edit(movie);
        ServletUtils.renderJson(resp,Map.of("success",edit));
    }
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String[] ids = req.getParameterValues("ids");
        List<Integer> integers = new ArrayList<>();
        for (String id : ids) {
            integers.add(Integer.parseInt(id));
        }
        int row=movieService.deleteByIds(integers);
        ServletUtils.renderJson(resp,Map.of("rows",row));
    }
}
